﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Net;

namespace AgencyManagerSystem.Controllers
{
    [Obsolete("暂不启用该功能")]
    public class DocTypeController : MyControllerBase
    {
        DocTypeManager manager;
        DocTypeFetcher fetcher;
        public DocTypeController() : base()
        {
            this.manager = new DocTypeManager();
            this.fetcher = new DocTypeFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Create)]
        public AsmObject<DocTypeModel> Set(DocTypeModel docTypeModel)
        {
            if (docTypeModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<DocTypeModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{1}]{0}", Util.ConvertToJson(docTypeModel), RequestIp));
            var result = manager.Set(docTypeModel);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Center, AppOpt.Delete)]
        public AsmObject<bool> Del(DocTypeModel docType)
        {
            if (docType == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(docType), RequestIp));
            var result = manager.Delete(docType);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( true);
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<DocTypeModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            var docTypeList = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<DocTypeModel>() { Data = docTypeList, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmObject<DocTypeModel> Get(int did)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", Util.ConvertToJson(did), RequestIp));
            var docTypeModel = fetcher.Get(did);
            DBHelper.Commit( true);
            AsmObject<DocTypeModel> result = null;
            if (docTypeModel != null)
                result = new AsmObject<DocTypeModel>() { Data = docTypeModel, Message = "操作成功", Result = AmsCode.Succ };
            else
                result = new AsmObject<DocTypeModel>() { Data = docTypeModel, Message = "无法找到数据", Result = AmsCode.DataNotFound };
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<DocTypeModel> ListByType(int type)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYTYPE:[RQS|{1}]{0}", Util.ConvertToJson(type), RequestIp));
            var list = fetcher.ListByType(type);
            DBHelper.Commit( true);
            var result = new AsmArray<DocTypeModel>() { Data = list, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYTYPE:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}